//
//  UUProductModel.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 26/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUProductModel.h"
#import <JSONKit_NoWarning/JSONKit.h>

@implementation UUProductSpecPeakSeasonModel

@end

@implementation UUProductSpecModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addManualMappingDict:@{
                                     @"type" : @"packageType",
                                     @"specId" : @"packageId",
                                     
                                     @"packageType" : @"type",
                                     @"packageId" : @"spec_id",
                                     }];
    }
    return self;
}

- (Class)classAtInsideOfObjectWithProperty:(NSString *)propertyName {
    if ([propertyName isEqualToString:@"peakSeason"] || [propertyName isEqualToString:@"halfDayPeakSeason"]) {
        return [UUProductSpecPeakSeasonModel class];
    } else {
        return [super classAtInsideOfObjectWithProperty:propertyName];
    }
}

@end

@implementation UUProductModel

+ (NSString *)getPrimaryKey {
    return @"id";
}

+ (void)initialize {
    if (self == [UUProductModel class]) {
        [self setUserCalculateForCN:@"spec"];
    }
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addManualMappingDict:@{
                                     @"busCategoryId" : @"carCategoryId",
                                     @"motorcadeId" : @"fleetId",
                                     }];
    }
    return self;
}

- (id)userGetValueForModel:(LKDBProperty *)property {
    NSMutableArray<NSDictionary *> *dicts = [NSMutableArray arrayWithCapacity:self.spec.count];
    for (UUProductSpecModel *model in self.spec) {
        [dicts addObject:[model toDictionary]];
    }
    
    NSString *JSON = [dicts JSONString];
    return JSON;
}

- (void)userSetValueForModel:(LKDBProperty *)property value:(id)value {
    NSArray<NSDictionary *> *dicts = [value objectFromJSONString];
    NSMutableArray<UUProductSpecModel *> *models = [NSMutableArray arrayWithCapacity:dicts.count];
    for (NSDictionary *dict in dicts) {
        UUProductSpecModel *model = [[UUProductSpecModel alloc] initWithData:dict];
        [models addObject:model];
    }
    
    self.spec = models;
}

- (Class)classAtInsideOfObjectWithProperty:(NSString *)propertyName {
    if ([propertyName isEqualToString:@"spec"]) {
        return [UUProductSpecModel class];
    } else {
        return [super classAtInsideOfObjectWithProperty:propertyName];
    }
}

@end
